package com.zczc.tagmanagement.web.controller;


import com.github.pagehelper.PageInfo;
import com.yishuifengxiao.common.tool.response.Response;
import com.yishuifengxiao.common.tool.response.Response.Const;
import com.yishuifengxiao.common.tool.validate.domain.Create;
import com.yishuifengxiao.common.tool.validate.domain.Update;
import com.zczc.tagmanagement.dto.TagDTO;
import com.zczc.tagmanagement.dto.TagSortsDTO;
import com.zczc.tagmanagement.dto.TagUpdateDTO;
import com.zczc.tagmanagement.entity.Tag;
import com.zczc.tagmanagement.vo.TagVO2;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author:Mark
 * @Description:
 * @Date: Created on 16:22 2018-12-10
 * @Modify by:
 */
@RestController
@RequestMapping("/zczc/tag")
@Api(tags = "众筹众创Tags标签管理")
@Validated

public class TagController extends BaseController {

  private static final Logger log= LoggerFactory.getLogger(TagController.class);


  @ApiOperation(value = "增加一个tag标签")
  @PostMapping("/addTag")
  Response addTag(
      @ApiParam(value = "选择性要添加的tag对象,id、add_time服务端已处理，数量都默认为0", name = "tagDTO", required = true)
      @RequestBody(required = true) @Validated(Create.class) TagDTO tagDTO,
      BindingResult bindingResult
  ) {
    if (bindingResult.hasErrors()) {
      return Response.badParam(bindingResult.getAllErrors().get(0).getDefaultMessage());
    }

    try {
      tagService.addTag(tagDTO);
    } catch (Exception e) {
      log.info(e.getMessage());
      return Response.badParam("失败");
    }
    return Response.suc("操作成功");

  }

  @ApiOperation(value = "根据分类查询标签")
  @GetMapping("/selectTagsBySorts")
  Response<PageInfo<TagVO2>> selectTagsBySorts(
      @ApiParam(value = "页大小，默认为20", name = "pageSize", required = false,defaultValue = "20") @RequestParam(value = "pageSize", required = false, defaultValue = "20") @Min(value = 1, message = "页大小最小为1") Integer pageSize,
      @ApiParam(value = "起始页，默认为第一页", name = "startPage", required = false,defaultValue = "1") @RequestParam(value = "startPage", required = false, defaultValue = "1") @Min(value = 1, message = "起始页最小为1") Integer startPage,
      @ApiParam(name = "sortsId",value = "分类的id",required = false) @RequestParam(required = false,value = "sortsId") String sortsId
  ) {
    TagSortsDTO sortsDTO=new TagSortsDTO(convert(sortsId),null,null);
    return new Response<PageInfo<TagVO2>>(Const.CODE_OK,Const.MSG_OK,tagService.selectTagsBySrots(sortsDTO,startPage,pageSize));
  }

  @ApiOperation(value = "根据id值选择性的修改某些字段")
  @PutMapping("/updateTagsSelective")
  Response updateTagsSelective(
      @ApiParam(value = "要修改的实体，其中id值必须给定", name = "updateDTO", required = true) @RequestBody(required = true) @Validated(Update.class) TagUpdateDTO updateDTO,BindingResult errors) {
    if(errors.hasErrors()){
      return Response.badParam(errors.getAllErrors().get(0).getDefaultMessage());
    }

    try {
      tagService.updateTagByIdSelective(updateDTO);
    } catch (Exception e) {
      log.info(e.getMessage());
      return Response.error("添加失败");
    }

    return Response.suc();
  }

  @ApiOperation(value = "根据id值进行逻辑删除")
  @DeleteMapping(value = "/deleteTagsLogic")
  Response deleteTagsLogic(
      @ApiParam(value = "要删除的id值", name = "ids", required = true) @RequestBody(required = true) @NotEmpty(message = "id不能为空") List<String> ids,BindingResult errors) {
    if(errors.hasErrors()){
      return Response.badParam(errors.getAllErrors().get(0).getDefaultMessage());
    }

    try {
      tagService.deleteTagLogic(convert(ids));
    } catch (Exception e) {
      log.info(e.getMessage());
      return Response.error("删除失败");
    }
    return Response.suc();
  }

  @ApiOperation(value = "根据条件进行分页查询")
  @GetMapping("/selectTagsByPage")
  Response<PageInfo<Tag>> selectTagsByPage(
      @ApiParam(value = "页大小，默认为20", name = "pageSize", required = false,defaultValue = "20") @RequestParam(value = "pageSize", required = false, defaultValue = "20") @Min(value = 1, message = "页大小最小为1") Integer pageSize,
      @ApiParam(value = "起始页，默认为第一页", name = "startPage", required = false,defaultValue = "1") @RequestParam(value = "startPage", required = false, defaultValue = "1") @Min(value = 1, message = "起始页最小为1") Integer startPage,
      @ApiParam(value = "标签名字", name = "tagName", required = false) @RequestParam(required = false, value = "tagName") String tagName
      ) {
    Tag tag=new Tag();
    tag.setTagName(convert(tagName));
    PageInfo<Tag> tagPageInfo = tagService.selectTagsByPage(tag, pageSize, startPage);
    return new Response<PageInfo<Tag>>(Const.CODE_OK,Const.MSG_OK,tagPageInfo);
  }

  @ApiOperation(value = "根据条件搜索标签")
  @GetMapping("/searchTagsByPage")
  Response<PageInfo<Tag>> searchTagsByPage(
      @ApiParam(value = "标签名字", name = "tagName", required = false) @RequestParam(required = false, value = "tagName") String tagName,
      @ApiParam(value = "页大小，默认为20", name = "pageSize", required = false,defaultValue = "20") @RequestParam(value = "pageSize", required = false, defaultValue = "20") @Min(value = 1,message = "页大小最小为1") Integer pageSize,
      @ApiParam(value = "起始页，默认为第一页", name = "startPage", required = false,defaultValue = "1") @RequestParam(value = "startPage", required = false, defaultValue = "1") @Min(value = 1,message = "起始页最小为1") Integer startPage) {
    Tag tag = new Tag();
    tag.setTagName(tagName);
    PageInfo<Tag> tagPageInfo = tagService.selectTagsByPage(tag, pageSize, startPage);
    return new Response<PageInfo<Tag>>(Const.CODE_OK,Const.MSG_OK,tagPageInfo);
  }

}
